package com.example.androidlearning.sy11.sy2dict;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.androidlearning.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class DictResolverActivity extends AppCompatActivity {

    ContentResolver contentResolver;
    Button insert;
    Button search;
    EditText etWord;
    EditText etDetail;
    EditText etKey;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sy1102_main);
        init();
        setListener();
    }


    private void init() {

        contentResolver = getContentResolver();
        insert = findViewById(R.id.sy11_add);
        search = findViewById(R.id.sy11_search);
        etWord = findViewById(R.id.sy11_word);
        etDetail = findViewById(R.id.sy11_detail);

    }

    private void setListener() {
        insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String word = etWord.getText().toString();
                String detail = etDetail.getText().toString();
                ContentValues contentValues = new ContentValues();
                contentValues.put(Words.Word.WORD, word);
                contentValues.put(Words.Word.DETAIL, detail);
                contentResolver.insert(Words.Word.DICT_CONTENT_URI, contentValues);
                Toast.makeText(DictResolverActivity.this, "添加生词成功", Toast.LENGTH_LONG).show();
            }
        });

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String key = etWord.getText().toString();
                Cursor cursor = contentResolver.query(Words.Word.DICT_CONTENT_URI,
                        null,
                        "word like ? or detail like ?" ,
                        new String[]{"%" + key + "%","%" + key + "%", },
                        null
                );
                Bundle data = new Bundle();
                data.putSerializable("data", convertCursorToList(cursor));

                Intent intent = new Intent(DictResolverActivity.this, ResultActivity.class);
                intent.putExtras(data);
                startActivity(intent);
            }
        });
    }

    public static ArrayList<Map<String, String>> convertCursorToList(Cursor cursor) {
        ArrayList<Map<String, String>> result = new ArrayList<Map<String, String>>();
        // 遍历Cursor结果集
        while (cursor.moveToNext()) {
            // 将结果集中的数据存入ArrayList中
            Map<String, String> map = new HashMap<>();
            // 取出查询记录中除了密码列的值
            map.put(Words.Word._ID, cursor.getString(0));
            map.put(Words.Word.WORD, cursor.getString(1));
            map.put(Words.Word.DETAIL, cursor.getString(2));
            result.add(map);
        }
        return result;
    }
}
